package org.ricks.ioc.utils;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.concurrent.TimeUnit;

public class StopWatch {

	private final long startTime = SystemClock.INSTANCE.currentTimeMillis();

	public long cost() {
		return SystemClock.INSTANCE.currentTimeMillis() - startTime;
	}

	/**
	 * 从StopWatch被创建，到调用这个方法消耗的时间
	 *
	 * @return 返回消耗的时间，保留两位小数，格式xx.xx
	 */
	public String costSeconds() {
		var cost = cost() / (float) TimeUnit.SECONDS.toMillis(1);
		var decimal = new BigDecimal(cost);
		return decimal.setScale(2, RoundingMode.HALF_UP).toString();
	}

	public String costMinutes() {
		var cost = cost() / (float) TimeUnit.MINUTES.toMillis(1);;
		var decimal = new BigDecimal(cost);
		return decimal.setScale(2, RoundingMode.HALF_UP).toString();
	}

	public long getStartTime() {
		return startTime;
	}
}
